/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * wyszukaniePolaczenia.java
 *
 * Created on 2011-04-26, 17:38:48
 */
package Interface;

import org.db2.project.airline.dao.DAOException;
import org.db2.project.airline.dto.FlightPlanQueryCriteria;
import org.db2.project.airline.model.Airport;
import org.db2.project.airline.model.FlightClassType;
import org.db2.project.airline.service.AirportService;
import org.db2.project.airline.service.FlightClassTypeService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Martyna
 */
public class wyszukaniePolaczenia extends javax.swing.JFrame {

    private AirportService airportService = new AirportService();

    private FlightClassTypeService flightClassTypeService = new FlightClassTypeService();

    private ContactEditorUI contactEditorUI;

    /**
     * Creates new form wyszukaniePolaczenia
     */
    public wyszukaniePolaczenia(ContactEditorUI mainMenu) {
        contactEditorUI = mainMenu;
        initComponents();

        fillComponentsData();
    }

    private void fillComponentsData() {
        Date currentDate = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        DataWylotuTextField.setValue(currentDate);
        DataPrzylotuTexyField.setValue(currentDate);

        try {
            List<Airport> airports = airportService.findAll();
            List<FlightClassType> flightClassTypes = flightClassTypeService.findAll();

            for (FlightClassType flightClassType : flightClassTypes) {
                klasaComboBox.addItem(flightClassType);
            }

            for (Airport airport : airports) {
                wylotComboBox.addItem(airport);
                przylotComboBox.addItem(airport);
            }

            if (przylotComboBox.getModel().getSize() > 1) {
                przylotComboBox.setSelectedIndex(1);
            }
        } catch (DAOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
     * content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        DataWylotuTextField = new javax.swing.JFormattedTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        DataPrzylotuTexyField = new javax.swing.JFormattedTextField();
        dwadniCheckbox = new javax.swing.JCheckBox();
        jLabel5 = new javax.swing.JLabel();
        iloscComboBox = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        klasaComboBox = new javax.swing.JComboBox();
        wObieStronyButton = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jButton1 = new javax.swing.JButton();
        wylotComboBox = new javax.swing.JComboBox();
        przylotComboBox = new javax.swing.JComboBox();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Wyszukaj Połączenie"));

        jLabel1.setText("Wylot:");

        jLabel2.setText("Przylot:");

        DataWylotuTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter()));
        DataWylotuTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DataWylotuTextFieldActionPerformed(evt);
            }
        });

        jLabel3.setText("Data Wylotu:");

        jLabel4.setText("Data Przylotu:");

        DataPrzylotuTexyField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter()));

        dwadniCheckbox.setText("+/- 2 dni");
        dwadniCheckbox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dwadniCheckboxActionPerformed(evt);
            }
        });

        jLabel5.setText("Ilość Miejsc:");

        iloscComboBox.setEditable(true);
        iloscComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"1", "2", "3", "4", "5", "6", "7", "8"}));
        iloscComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                iloscComboBoxActionPerformed(evt);
            }
        });

        jLabel6.setText("Klasa:");

        klasaComboBox.setEditable(true);

        buttonGroup1.add(wObieStronyButton);
        wObieStronyButton.setSelected(true);
        wObieStronyButton.setText("Lot w obie strony");
        wObieStronyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                wObieStronyButtonActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText("Lot w jedną stronę");

        jButton1.setText("Wyszukaj");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Wstecz");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addComponent(wylotComboBox, 0, 306, Short.MAX_VALUE))
                                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addComponent(przylotComboBox, 0, 306, Short.MAX_VALUE))
                                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                                        .addComponent(jLabel4)
                                                                        .addComponent(jLabel3))
                                                                .addGap(18, 18, 18)
                                                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                                        .addComponent(DataWylotuTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(DataPrzylotuTexyField, javax.swing.GroupLayout.Alignment.LEADING)))
                                                        .addComponent(dwadniCheckbox)
                                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                                        .addComponent(jLabel5)
                                                                        .addComponent(jLabel6))
                                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                                        .addComponent(iloscComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(klasaComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                                .addContainerGap())
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(wObieStronyButton)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 82, Short.MAX_VALUE)
                                                .addComponent(jRadioButton2)
                                                .addGap(60, 60, 60))
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(jButton1)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 177, Short.MAX_VALUE)
                                                .addComponent(jButton2)
                                                .addGap(43, 43, 43))))
        );
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel1)
                                        .addComponent(wylotComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel2)
                                        .addComponent(przylotComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(23, 23, 23)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(wObieStronyButton)
                                        .addComponent(jRadioButton2))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel3)
                                        .addComponent(DataWylotuTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel4)
                                        .addComponent(DataPrzylotuTexyField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(dwadniCheckbox)
                                                .addGap(10, 10, 10)
                                                .addComponent(jLabel5))
                                        .addComponent(iloscComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel6)
                                        .addComponent(klasaComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jButton1)
                                        .addComponent(jButton2)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addContainerGap())
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void dwadniCheckboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dwadniCheckboxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_dwadniCheckboxActionPerformed

    private void iloscComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_iloscComboBoxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_iloscComboBoxActionPerformed

    private void wObieStronyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_wObieStronyButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_wObieStronyButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        Airport Przylot, Wylot;
        FlightClassType klasa;
        Date DataWylotu, DataPrzylotu;
        boolean wObieStrony, dwaDniCheckbox;
        int iloscMiejsc;

        SimpleDateFormat dateFormat = new SimpleDateFormat();

        Wylot = (Airport) wylotComboBox.getSelectedItem();
        Przylot = (Airport) przylotComboBox.getSelectedItem();

        DataWylotu = (Date) DataWylotuTextField.getValue();
        DataPrzylotu = (Date) DataPrzylotuTexyField.getValue();
        wObieStrony = wObieStronyButton.isSelected();
        dwaDniCheckbox = dwadniCheckbox.isSelected();
        iloscMiejsc = Integer.parseInt(iloscComboBox.getSelectedItem().toString());
        klasa = (FlightClassType) klasaComboBox.getSelectedItem();

        System.out.println(klasa);
        if (wObieStrony) System.out.println("W obie strony");
        if (!wObieStrony) System.out.println("W jedna strone");
        if (dwaDniCheckbox) System.out.println("dwa dni plus minus");

        FlightPlanQueryCriteria flightPlanQueryCriteria = new FlightPlanQueryCriteria(Wylot, Przylot,
                DataWylotu, DataPrzylotu, !wObieStrony, dwaDniCheckbox, iloscMiejsc, klasa);

        new WynikWyszukiwania(flightPlanQueryCriteria, this).setVisible(true);
        setVisible(false);
        //wywoaj metode  a tymi argumnetami szukajaca w bazie polaczenia

    }//GEN-LAST:event_jButton1ActionPerformed

    private void DataWylotuTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DataWylotuTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_DataWylotuTextFieldActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        contactEditorUI.setVisible(true);
        this.setVisible(false);// TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                //new wyszukaniePolaczenia(contactEditorUI).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFormattedTextField DataPrzylotuTexyField;
    private javax.swing.JFormattedTextField DataWylotuTextField;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JCheckBox dwadniCheckbox;
    private javax.swing.JComboBox iloscComboBox;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JComboBox klasaComboBox;
    private javax.swing.JComboBox przylotComboBox;
    private javax.swing.JRadioButton wObieStronyButton;
    private javax.swing.JComboBox wylotComboBox;
    // End of variables declaration//GEN-END:variables
}
